// pages/index/wode/wode.js
let app = getApp();
import ajax from "../../../utils/ajax";
let util = require("../../../utils/util.js");
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    myinfo: { // 属性名
      type: Object, // 类型（必填），目前接受的类型包括：String, Number, Boolean, Object, Array, null（表示任意类型）
      value: '', // 属性初始值（可选），如果未指定则会根据类型选择一个
      observer(newVal, oldVal, changedPath) {
        if(newVal.auth_status==3&&newVal.role==2){
          newVal.real_name=newVal.company.name
          newVal.headimgurl=newVal.company.logo
        }
        this.setData({
          user_info: newVal
        })
        this.update_auth_status()
      }
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    menu_list1: [
      {
        name: '我的零钱',
        icon: '/image/wode/p1.png',
        path: '/pages/user/my_money/my_money',
        show: true
      },
      {
        name: '我的报名',
        icon: '/image/wode/p2.png',
        path: '/pages/user/wode_baoming/wode_baoming/wode_baoming',
        show: true
      },
      {
        name: '我的发布',
        icon: '/image/wode/p12.png',
        path: '/pages/user/my_publish_list/my_publish_list',
        show: true
      },
      
      {
        name: '我的简历',
        icon: '/image/wode/p4.png',
        path: '/pages/other/coding/coding',
        show: true
      },
      {
        name: '我的认证',
        type: 'renzheng',
        status_text: '暂无任何认证',
        icon: '/image/wode/p9.png',
        path: '',
        show: true
      },
      {
        name: '报名管理',
        icon: '/image/wode/p3.png',
        path: '/pages/user/qiye_baoming_liebiao/qiye_baoming_liebao',
        show: true
      },
      {
        name: '支付违约金',
        icon: '/image/wode/p11.png',
        path: '/pages/user/weiyuejin_zhifu/weiyuejin_zhifu',
        show: false
      },
    ],
    user_info: {
      headimgurl: 'http://img.gek6.com/FnzCBkjI6ByH69cvpiYS8XMdXytx',
      nickname: '未登录',
    },
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 登录
    login_func() {
      console.log("login_func");
      let that = this;
      that.wx_login_code().then(code => {
        console.log("wx_login_code-->" + code);
        ajax.post({
          url: 'wx/login',
          data: {
            code
          }
        }).then(res => {

          if (res.code == 0) {
            // 登录成功
            console.log(res);
            // 保存token
            wx.setStorageSync('token', res.data.token)
            app.common.token = res.data.token;
            app.toast("登录成功");
            // 更新用户数据
            this.triggerEvent('updateuserinfo', {}, {})
          } else {
            // 登录失败
            console.log("登录失败")
            // 注册
            console.log("去注册")
            console.log(res);
            that.register(code)

          }
        })
      })
    },
    // 获取登录授权code
    wx_login_code() {
      return new Promise(function (resolve, reject) {
        wx.login({
          success(data) {
            if (data.code) {
              console.log(data.code)
              resolve(data.code)
            } else {
              console.log('登录授权失败！' + res.errMsg)
              reject(res.errMsg)
            }

          }
        })
      })
    },
    // 获取用户信息加密数据
    wx_get_user_info() {
      return new Promise(function (resolve, reject) {
        wx.getUserInfo({
          success: function (res) {
            console.log(res)
            resolve(res)
          },
          fail: function (err) {
            console.log(err)
          }
        })
      })
    },
    // 注册
    register(code) {
      let that = this;
      that.wx_login_code().then(code => {
        that.wx_get_user_info().then(user => {
          ajax.post({
            url: 'wx/register',
            data: {
              code,
              rawData: user.rawData,
              signature: user.signature,
              encryptedData: user.encryptedData,
              iv: user.iv
            }
          }).then(res => {
            console.log(res);
            if (res.code == 0) {
              console.log("注册成功");
              that.login_func();
            }
          })
        })
      })
    },
    // 去绑定手机
    to_bind_mobile() {
      wx.navigateTo({
        url: '/pages/index/wode/bind_phone/bind_phone'
      })
    },
    // 跳转页面
    go_page(e) {
      if (!app.common.token) {
        app.toast("请先登陆");
        return;
      }
      let item = this.data.menu_list1[e.currentTarget.dataset.index];
      console.log(item);

      // 测试支付
      if (item.path == "pay") {

        ajax.post({
          url: 'wx/pay',
          data: {
            money: "0.01",
            type: 1
          }
        }).then(res => {
          wx.requestPayment({
            timeStamp: res.data.timeStamp,
            nonceStr: res.data.nonceStr,
            package: res.data.package,
            signType: res.data.signType,
            paySign: res.data.paySign,
            success(res) {
              console.log(res);
              app.toast("支付成功", 'succ')
            },
            fail(res) {
              console.log(res)

            }
          })
        })

        return;
      };


      if (e.currentTarget.dataset.index == 4) {
        // 认证入口
        this.auth_enter()
        return;
      }
      wx.navigateTo({
        url: item.path
      })
    },
    // 认证入口
    auth_enter() {
      let status = this.data.user_info.auth_status;
      let role = this.data.user_info.role;
      // 未认证状态  选择认证方式
      if (status == 1) {
        wx.navigateTo({
          url: '/pages/user/select_auth_type/select_auth_type'
        })

      }
      // 认证中 
      if (status == 2) {
        app.toast("认证资料审核中")
      }
      // 认证通过
      if (status == 3) {
        // 查看认证资料
        if (role == 1) {
          wx.navigateTo({
            url: '/pages/user/geren_xinxi/geren_xinxi'
          })
        } else {
          wx.navigateTo({
            url: '/pages/user/qiyexinxi/qiyexinxi'
          })
        }
      }
      // 认证失败
      if (status == 4) {

      }

    },
    // 更新认证状态
    update_auth_status() {
      console.log("update_auth_status");
      let menu_list1 = this.data.menu_list1;
      let status = this.data.user_info.auth_status;
      let role = this.data.user_info.role;
      if (status == 1) {

        // 点击时需选择 个人/企业

      }
      if (status == 2) {
        if (role == 1) {
          menu_list1[4].status_text = "个人认证(审核中)";
        } else {
          menu_list1[4].status_text = "企业认证(审核中)";
        }
        menu_list1[4].path = "";
        this.setData({
          menu_list1
        })
      }
      if (status == 3) {
        console.log("已认证")
        if (role == 1) {
        console.log("已认证 个人")

          menu_list1[4].status_text = "个人已认证";
          
        } else {
        console.log("已认证 企业")

          menu_list1[4].status_text = "企业已认证";
        }
        menu_list1[4].path = "";
        this.setData({
          menu_list1
        })
      }
      if (status == 4) {
        if (role == 1) {
          menu_list1[4].name = "个人认证(审核失败，请重新提交)";
        } else {
          menu_list1[4].name = "企业认证(审核失败，请重新提交)";
        }

        menu_list1[4].path = "";
        this.setData({
          menu_list1
        })
      }


       
    },
    // 更新 权限 菜单
    update_menu_list() {
      let user_info = this.data.user_info;
      let menu_list1 = this.data.menu_list1;
      if (user_info.role == 2) {
        // 2企业
        menu_list1[1].show = false;
        menu_list1[3].show = false;
      }else{
        menu_list1[2].show = false;
        menu_list1[5].show = false;
      }
      if(user_info.liquidated_damages){
        menu_list1[6].show = true;
      }
      this.setData(menu_list1)
    }
  },
  attached() {
    this.update_menu_list()
  }
})
